﻿using JyUserFeedback;
using JyUserFeedback.view;
using JyUserInfo;
using JyUserInfo.Model;
using System;
using System.Linq;
using Windows.ApplicationModel.Activation;
using Windows.Storage.Pickers;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Navigation;

namespace JyFeedbackDemo.WindowsPhone81
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : IContinueFileOpen
    {
        private readonly JyUserFeedbackSDKManager _jyUserFeedbackSdkManager = new JyUserFeedbackSDKManager();

        private UserInfo _userInfo;

        public MainPage()
        {
            InitializeComponent();

            NavigationCacheMode = NavigationCacheMode.Required;

            JyUserInfoManager.LogincompletedEvent += JyUserInfoManager_LoginCompleted;

            JyFeedbackControl.FeedbackImageRequested += delegate
            {
                var fileOpenPicker = new FileOpenPicker();
                fileOpenPicker.FileTypeFilter.Add(".png");
                fileOpenPicker.FileTypeFilter.Add(".jpg");

                fileOpenPicker.PickSingleFileAndContinue();
            };
        }

        public void ContinueFileOpen(FileOpenPickerContinuationEventArgs args)
        {
            var file = args.Files.ElementAtOrDefault(0);
            if (file != null)
            {
                _jyUserFeedbackSdkManager.UploadPicture(Constants.DemoAppkey, Constants.DemoSecretId, file);
            }
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。
        /// 此参数通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: 准备此处显示的页面。

            // TODO: 如果您的应用程序包含多个页面，请确保
            // 通过注册以下事件来处理硬件“后退”按钮:
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。
            // 如果使用由某些模板提供的 NavigationHelper，
            // 则系统会为您处理该事件。
        }

        private async void FastLoginButton_Click(object sender, RoutedEventArgs e)
        {
            var userInfo = await JyUserInfoManager.QuickLogin(Constants.DemoAppkey);
            if (userInfo.isLoginSuccess)
            {
                _userInfo = userInfo;
                await new MessageDialog(Constants.FastLoginSuccessMessage).ShowAsync();
            }
            else
            {
                await new MessageDialog(Constants.FastLoginFailedMessage).ShowAsync();
            }
        }

        private async void GetDeveloperReplyCountButton_Click(object sender, RoutedEventArgs e)
        {
            if (_userInfo == null)
            {
                await new MessageDialog(Constants.PleaseLoginFirstMessage).ShowAsync();
            }
            else
            {
                var newFeedBackRemindCount = await _jyUserFeedbackSdkManager.GetNewFeedBackRemindCount(Constants.DemoAppkey, _userInfo.U_Key);
                await new MessageDialog(string.Format(Constants.NewFeedbackReplyCountMessage, newFeedBackRemindCount)).ShowAsync();
            }
        }

        private void JyUserInfoManager_LoginCompleted(object sender, UserInfo userInfo)
        {
            if (userInfo.isLoginSuccess)
            {
                _userInfo = userInfo;
            }
            else
            {
                // 不需要在 else 逻辑中弹出对话框显示登录失败，登录失败时，登录控件会有弹窗提示。
            }
        }

        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            JyUserInfoManager.ShowLogin(RootGrid, Constants.DemoAppkey, LoginTemplate.Template3);
        }

        private async void OpenFeedbackWindowButton_Click(object sender, RoutedEventArgs e)
        {
            if (_userInfo == null)
            {
                await new MessageDialog(Constants.PleaseLoginFirstMessage).ShowAsync();
            }
            else
            {
                _jyUserFeedbackSdkManager.ShowFeedBack(RootGrid, false, Constants.DemoAppkey, _userInfo.U_Key);
            }
        }
    }
}